package mx.com.theboard.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;

import com.google.gwt.core.client.GWT;

public class FileUpload extends HttpServlet {
	  private static final Logger log =Logger.getLogger(FileUpload.class.getName());

	  public void doPost(HttpServletRequest req, HttpServletResponse res)
	      throws ServletException, IOException {
	    try {
	      ServletFileUpload upload = new ServletFileUpload();
	      res.setContentType("text/plain");

	      FileItemIterator iterator = upload.getItemIterator(req);
	      while (iterator.hasNext()) {
	        FileItemStream item = iterator.next();	        
	        InputStream stream = item.openStream();

	        if (item.isFormField()) {
	          log.warning("Got a form field: " + item.getFieldName());
	        } else {
	          log.warning("Got an uploaded file: " + item.getFieldName() +
	                      ", name = " + item.getName());

	          // You now have the filename (item.getName() and the
	          // contents (which you can read from stream). Here we just
	          // print them back out to the servlet output stream, but you
	          // will probably want to do something more interesting (for
	          // example, wrap them in a Blob and commit them to the
	          // datastore).
	          int len;
	          byte[] buffer = new byte[8192];
	          File uploadedFile = new File(req.getContextPath(), "uploads/"+item.getName());	          
	          while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
	        	
	            //res.getOutputStream().write(buffer, 0, len);
	          }
	          
	        }
	      }
	    } catch (Exception ex) {
	      throw new ServletException(ex);
	    }
	  }
	}